package com.aaa.web.service.impl.carhouseserviceimpl;

import com.aaa.entity.BackEntrepotRecord;
import com.aaa.entity.repair.BackEntrepot;
import com.aaa.phone.systemconst.ISystemConst;
import com.aaa.util.EmpUtil;
import com.aaa.web.commons.NewCarQuery;
import com.aaa.web.commons.WlCommonsCode;
import com.aaa.web.dao.icarhousedao.IPartsHouseDao;
import com.aaa.web.service.icarhouseservice.IPartsHouseService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: battery
 * @description: 配件仓库
 * @author: 王璐
 * @create: 2018-12-22 08:42
 **/
@Service
@Transactional
public class PartsHouseServiceImpl implements IPartsHouseService, ISystemConst {
    /**
     * 依赖注入
     */
    @Autowired
    private IPartsHouseDao dao;

    /**
     * 获取所有的配件
     * @param query
     * @return
     */
    @Override
    public Map getAllParts(NewCarQuery query) {

        //调用公用代码，返回学校id
        int isAdminSchoolId = WlCommonsCode.isAdmin();
        int size = query.getLimit();
        int page = query.getPage();
        PageHelper.startPage(page,size);
        Map<String,Object> map = new HashMap<>();
        //数据
        map.put("data",dao.getAllParts(isAdminSchoolId));
        //数据总量
        map.put("count",dao.getPartsCount(isAdminSchoolId));
        //是否是超管  0 是超管
        map.put("isAdmin", isAdminSchoolId);
        //数据总量
        map.put("schoolId", EmpUtil.getEmp().getSchoolid());
        map.put("code",ZERO);
        return map;
    }
    /**
     * 查询配件
     * @param query
     * @return
     */
    @Override
    public Map<String, Object> selectParts(NewCarQuery query, BackEntrepot backEntrepot) {
        Map<String, Object> m = new HashMap<>();
        //调用公用代码，返回学校id
        int schoolId = WlCommonsCode.getSchoolId(backEntrepot);
        Map<String, Object> map = new HashMap<>();
        Map<Object, Object> paramMap = new HashMap<>();
        paramMap.put("goodsName",backEntrepot.getGoodsname());
        paramMap.put("schoolId",schoolId);
        List<Map> allCar = dao.selectParts(paramMap);
        Integer carCount = dao.getSelectPartsCount(paramMap);
        int size = query.getLimit();
        int page = query.getPage();
        PageHelper.startPage(page, size);
        //数据
        m.put("data", allCar);
        //是否是超管  0 是超管
        m.put("isAdmin", WlCommonsCode.isAdmin());
        //数据总量
        m.put("count", carCount);
        //数据总量
        m.put("schoolId", EmpUtil.getEmp().getSchoolid());
        m.put("code", ZERO);
        return m;
    }
    /**
     * 添加
     * @param backEntrepot
     * @return
     */
    @Override
    public int addParts(BackEntrepot backEntrepot){
        //在session中获取员工的店名ID
        int schoolId = WlCommonsCode.isAdmin();
        backEntrepot.setSchoolId(schoolId);
        String goodsName = backEntrepot.getGoodsname();
        String operator = EmpUtil.getEmp().getName();
        //添加数据
        int add = dao.addParts(backEntrepot);
        Map<Object, Object> map = new HashMap<>();
        map.put("operator",operator);
        map.put("backEntrepot",backEntrepot);
        int addRecord = dao.addPartsRecord(map);
        //返回添加成功或失败数据
        return WlCommonsCode.result(add,addRecord);
    }

    /**
     * 补货
     * @param goodsName
     * @param goodsQuantity
     * @return
     */
    @Override
    public int addOneTypeParts(String goodsName,int goodsQuantity,double goodsCost){
        //在session中获取员工的店名ID
        int schoolId = WlCommonsCode.isAdmin();
        //在仓库表添加数据
        Map<Object, Object> map = new HashMap<>();
        map.put("goodsName",goodsName);
        map.put("schoolId",schoolId);
        map.put("goodsQuantity",goodsQuantity);
        //添加数据
        int addOne = dao.addOneTypeParts(map);
        //在仓库操作日志表添加数据
        String operator = EmpUtil.getEmp().getName();
        BackEntrepotRecord backEntrepotRecord = new BackEntrepotRecord(goodsName, goodsCost, goodsQuantity, operator, schoolId);
        int addRecord = dao.addOneTypePartsRecord(backEntrepotRecord);
        //返回添加成功或失败数据
        return WlCommonsCode.result(addOne,addRecord);
    }

    /**
     * 编辑
     * @param goodsName
     * @return
     */
    @Override
    public int editOneTypeParts(String goodsName,String oldGoodsName){
        int schoolId = WlCommonsCode.isAdmin();
        Map<Object, Object> map = new HashMap<>();
        map.put("goodsName",goodsName);
        map.put("schoolId",schoolId);
        map.put("oldGoodsName",oldGoodsName);
        int isUpdate = dao.editOneTypePartsAtRecord(map);
        int update = dao.editOneTypeParts(map);
        return WlCommonsCode.result(isUpdate,update);
    }
}
